package com.e2oto.seller.modules.seller.presenter;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.common.EasyAES;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.base.BasePresenter;
import com.e2oto.seller.common.base.LoadingDialog;
import com.e2oto.seller.common.utils.ImageCompressUtils;
import com.e2oto.seller.common.utils.RetrofitUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.seller.views.AddGoodsDetailView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by ZhangLiQiang on 2017/11/23.
 */

public class AddGoodsDetailPresenter extends BasePresenter<AddGoodsDetailView> {

    public void addGoodsThirdStep(List<String> images, String description, String goodsId) {

        final LoadingDialog loadingDialog = new LoadingDialog(getContext());

        List<String> oldImages = new ArrayList<>();//http开头
        List<String> addImages = new ArrayList<>();//本地路径开头
        for (int i = 0; i < images.size(); i++) {
            String path = images.get(i);
            if (path.startsWith("http")) {
                oldImages.add(path);
            } else {
                addImages.add(path);
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("description", description);
        jsonObject.put("goods_id", goodsId);
        jsonObject.put("old_content_images", oldImages.toArray());
        String param = EasyAES.encrypt(jsonObject);

        List<File> compressedImage;
        try {
            compressedImage = ImageCompressUtils.getCompressedImageList(addImages, getContext());
        } catch (Exception e) {
            e.printStackTrace();
            getView().showMessage(e.toString());
            return;
        }

        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (int i = 0; i < compressedImage.size(); i++) {
            File file = compressedImage.get(i);
            builder.addFormDataPart("img_" + i, file.getName(), RequestBody.create(MediaType.parse("image/jpeg"), file));
        }
        builder.addFormDataPart("param", param);
        builder.addFormDataPart("ver", SellerApp.versionName);


        RetrofitUtils.getApiService().addGoodsThirdStep(builder.build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        loadingDialog.show();
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        if (baseBean.getCode() == 200) {
                            getView().addSpecThridCallback();
                        } else {
                            getView().showMessage(baseBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        loadingDialog.dismiss();
                        getView().showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        loadingDialog.dismiss();
                    }
                });
    }
}
